//
//  MHInfoImagesScrollView.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/4/23.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHInfoImagesScrollView.h"
#import "MHCoverModel.h"
@interface MHInfoImagesScrollView()
@property(nonatomic, strong) UIScrollView *scrollView;
@end


@implementation MHInfoImagesScrollView

-(instancetype)initWithImages:(NSArray *) covers
{
    self = [super init];
    if (self) {
        self.covers = covers;
//        self.backgroundColor = [UIColor whiteColor];
        self.backgroundColor = [UIColor clearColor];
        self.scrollView = [UIScrollView new];
        __weak __typeof(self)weakSelf = self;
        [self addSubview:self.scrollView];
        self.scrollView.backgroundColor = [UIColor whiteColor];
        [self masLayoutSubViews];
    }
    return self;
}

-(void)bindViewModel:(id)viewModel
{
    
}


-(void)setCovers:(NSArray *)covers
{
    _covers  = covers;
    [self masLayoutSubViews];
}

- (void)masLayoutSubViews
{
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
        make.left.right.equalTo(self);
        make.height.mas_offset(100);
    }];
    [self.scrollView setContentSize:CGSizeMake((_covers.count)*100 + 20, 100)];
    self.scrollView.showsHorizontalScrollIndicator = NO;
     self.scrollView.showsVerticalScrollIndicator = NO;
    CGFloat space = 20;
    CGFloat hSpace = 15;
    CGFloat w = 80;
    for (int i = 0; i<self.covers.count; i++) {
        MHCoverModel *coverModel = self.covers[i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i*w+((i+1)*space), hSpace, w, w );
        [button setTitleColor:MH_MAIN_TEXT_COLOR_82 forState:0];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        button.titleLabel.font = UIFontFromPixel(24);
//        WEAKSELF
        __weak __typeof(self)weakSelf = self;
        [button bk_whenTapped:^{
            weakSelf.tapButtonBlock(coverModel);
        }];
        if (coverModel.fullCoverUrl && [coverModel.fullCoverUrl hasPrefix:@"http"]) {
            [button yy_setImageWithURL:[NSURL URLWithString:coverModel.fullCoverUrl] forState:0 placeholder:MHWebImagePlaceholder()];
        }else
        {
            [button setImage:[UIImage imageNamed:coverModel.fullCoverUrl] forState:0];
        }
        [self.scrollView addSubview:button];
    }
    
    
}

@end
